"""
隐患追踪管理后台配置

此模块配置Django后台管理界面的隐患追踪功能
"""

from django.contrib import admin
from .models import HazardTracking


@admin.register(HazardTracking)
class HazardTrackingAdmin(admin.ModelAdmin):
    """隐患追踪后台管理配置"""
    
    list_display = [
        'task_id', 'mine_name', 'indicator_name', 'safety_level', 
        'responsible_person', 'status', 'assigned_date', 'deadline_date',
        'safety_evaluation', 'is_overdue', 'days_remaining'
    ]
    
    list_filter = [
        'status', 'safety_level', 'assigned_date', 'deadline_date', 'safety_evaluation'
    ]
    
    search_fields = [
        'task_id', 'mine_name', 'indicator_name', 'responsible_person'
    ]
    
    readonly_fields = [
        'task_id', 'assigned_date', 'created_at', 'updated_at'
    ]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('task_id', 'mine_name', 'indicator_name', 'safety_level')
        }),
        ('整改信息', {
            'fields': ('trigger_condition', 'remediation_suggestion', 'responsible_person')
        }),
        ('状态管理', {
            'fields': ('status', 'assigned_date', 'deadline_date', 'completion_date')
        }),
        ('复核信息', {
            'fields': ('reviewer', 'review_comment')
        }),
        ('其他信息', {
            'fields': ('safety_evaluation', 'remarks', 'attachments', 'created_at', 'updated_at')
        }),
    )
    
    date_hierarchy = 'assigned_date'
    ordering = ['-assigned_date']
    
    def is_overdue(self, obj):
        """显示是否逾期"""
        return obj.is_overdue()
    is_overdue.boolean = True
    is_overdue.short_description = '是否逾期'
    
    def days_remaining(self, obj):
        """显示剩余天数"""
        return f"{obj.days_remaining()} 天"
    days_remaining.short_description = '剩余天数'

